<?php

namespace app\common\resourceHandle;

use app\common\model\GoodsImage;
use think\Model;

class OrderResource extends ResourceHandle
{

    /**
     * @param Model $model
     * @return array
     * @throws \think\Exception
     */
    public function handle(Model $model): array
    {
        $model->hidden(['shop_cost']);
        $order = $model->toArray();
        if(!empty($order['order_goods'])){
            $order['order_goods'] = $order['order_goods'][0];
            $goodsImage = GoodsImage::getFirstImage((int)$order['order_goods']['goods_id']) ?? '';
            $order['order_goods']['goods_image'] = $goodsImage;
        } else {
            $order['order_goods'] = '';
        }
        return $order;
    }

    public function info(Model $model): array
    {
        return $this->handle($model);
    }

    /**
     * 订单消费记录处理
     * @param Model $model
     * @return array
     * @throws \think\Exception
     */
    public function memberConsumeRecord(Model $model)
    {
        $model->car;
        $model->order_goods;
        $model->shop->visible(['id', 'shop_name']);
        return $this->handle($model);
    }

    /**
     * 商家消费记录
     * @param Model $model
     * @return array
     * @throws \think\Exception
     */
    public function shopConsumeRecord(Model $model)
    {
        $model->car;
        $model->order_goods;
        $order = $model->toArray();
        $order['order_goods'] = !empty($order['order_goods']) ? $order['order_goods'][0] : '';
        return $order;
    }
}